/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Canadian Weather.
 *
 * The Initial Developer of the Original Code is
 *   George Bradt.
 *
 * Portions created by the Initial Developer are Copyright (C) 2011
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   George Bradt.
 *
 * Credits:
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

const data = require("self").data;
const windowutils = require("window-utils");
const icon = data.url('images/sunny48.png');

/**
 * Create and position the notification box.
 * 
 * @returns {Object}    Custom notificationBox.
 */
function createNotificationBox() {
    // Create a notificationBox clone for insertion before the addon bar.
    var browser = windowutils.activeBrowserWindow.gBrowser;
    var myNotificationBox = browser.getNotificationBox().cloneNode(true);
    myNotificationBox.setAttribute('flex', '1');
    
    // Create a container for the notificationBox.
    // The content will appear on the right side, much like a toaster.
    var doc = browser.ownerDocument;
    var hbox = doc.createElement('hbox');
    var spacer = doc.createElement('spacer');
    spacer.setAttribute('flex', '9');
    spacer.style.setProperty('background-color', 'white', '');
    hbox.appendChild(spacer);
    hbox.appendChild(myNotificationBox);
    
    // Get nodes for insertion.
    var addonBar = doc.getElementById('addon-bar');
    var browserBottomBox = doc.getElementById('browser-bottombox');
    browserBottomBox.insertBefore(hbox, addonBar);
    
    return myNotificationBox;
}

/**
 * Display a notification using notification box.
 * 
 * @param   {Object} aNotificationBox   Custom notificationBox.
 * @param   {string} aText              Message to display.
 * @param   {string} aAlertLevel        Used to set notificationBox priority level.
 * @param   {Object} aPanel             Warnings panel (may be null).
 */
function displayNotification(aNotificationBox, aText, aAlertLevel, aPanel) {
    var buttons;
    var priority;
    
    if ( aPanel ) {
        aNotificationBox.removeAllNotifications(false);
        
        buttons = [{label: 'Details',  
                    accessKey: '',  
                    popup: null,  
                    callback: function() {
                        this.showDetails(aPanel);
                    }},
                   {label: 'Close',  
                    accessKey: '',  
                    popup: null,  
                    callback: function() {
                    }}];
    }
    else {
        buttons = [{label: 'Close',  
                    accessKey: '',  
                    popup: null,  
                    callback: function() {
                    }}];
    }
    
    switch ( aAlertLevel ) {
        case 'low':
            priority = aNotificationBox.PRIORITY_INFO_HIGH;
            break;

        case 'medium':
            // Yellow.
            priority = aNotificationBox.PRIORITY_WARNING_HIGH;
            break;

        case 'high':
            // Red.
            priority = aNotificationBox.PRIORITY_CRITICAL_HIGH;
            break;
        
        default:
            priority = aNotificationBox.PRIORITY_INFO_HIGH;
            break;
    }
    
    var notification = aNotificationBox.appendNotification(
        'CANADIAN WEATHER: ' + aText,
        'canadianWeather',
        icon,
        priority,
        buttons
    );
    
    if ( aPanel && aAlertLevel == 'medium' ) {
        notification.style.setProperty('background-image', 'none', 'important');
        notification.style.setProperty('background-color', 'yellow', 'important');
        notification.style.setProperty('color', 'black', 'important');
    }
    else if ( aPanel && aAlertLevel == 'high' ) {
        notification.style.setProperty('background-image', 'none', 'important');
        notification.style.setProperty('background-color', 'red', 'important');
        notification.style.setProperty('color', 'white', 'important');
    }
}
    
/**
 * Display the warnings panel when the Details button is clicked.
 * 
 * @param   {Object} aPanel     Warnings panel.
 */
function showDetails(aPanel) {
    aPanel.show();
}

// Exports.
exports.displayNotification = displayNotification;
exports.createNotificationBox = createNotificationBox;